package com.main.beatit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.*;
import android.widget.*;

public class SoundChange extends Activity {
	public static final int[] SMALLIMGID = { R.id.ivSmall1, R.id.ivSmall2, R.id.ivSmall3, R.id.ivSmall4, R.id.ivSmall5,
			R.id.ivSmall6, R.id.ivSmall7, R.id.ivSmall8, R.id.ivSmall9, R.id.ivSmall10, R.id.ivSmall11, R.id.ivSmall12,
			R.id.ivSmall13, R.id.ivSmall14, R.id.ivSmall15, R.id.ivSmall16 };

	public static final int[] CHANGEPADID = { R.id.ivPadChange1, R.id.ivPadChange2, R.id.ivPadChange3,
			R.id.ivPadChange4, R.id.ivPadChange5, R.id.ivPadChange6, R.id.ivPadChange7, R.id.ivPadChange8 };

	public static final int[] LAYSMALLID = { R.id.lay1, R.id.lay2, R.id.lay3, R.id.lay4, R.id.lay5, R.id.lay6,
			R.id.lay7, R.id.lay8 };

	public static final int[] BUTTONCHNID = { R.id.btnChange1, R.id.btnChange2, R.id.btnChange3, R.id.btnChange4,
			R.id.btnChange5, R.id.btnChange6, R.id.btnChange7, R.id.btnChange8 };

	public static final int[] LAYPADID = { R.id.llPads1, R.id.llPads2, R.id.llPads3, R.id.llPads4, R.id.llPads5,
			R.id.llPads6, R.id.llPads7, R.id.llPads8, R.id.llPads9, R.id.llPads10, R.id.llPads11, R.id.llPads12,
			R.id.llPads13, R.id.llPads14, R.id.llPads15, R.id.llPads16 };

	ImageView ivSmall[];
	ImageView ivPads[];
	ImageView ivButtons[];
	LinearLayout llSmall[];
	LinearLayout llPads[];

	int nPadIndex = 0;
	int[] nOldSOUNDID = { MainActivity.nSoundID, MainActivity.nSoundID + 1, MainActivity.nSoundID + 2,
			MainActivity.nSoundID + 3, MainActivity.nSoundID + 4, MainActivity.nSoundID + 5, MainActivity.nSoundID + 6,
			MainActivity.nSoundID + 7 };

	Display display; // Device display information
	int displayWidth;
	int displayHeight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.soundchange);

		// I don't use auto sleep mode.
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		// Get a display information.
		display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

		displayWidth = display.getWidth();
		displayHeight = display.getHeight();

		ivSmall = new ImageView[16];
		llPads = new LinearLayout[16];

		for (int i = 0; i < 16; i++) {
			ivSmall[i] = (ImageView) findViewById(SMALLIMGID[i]);
			llPads[i] = (LinearLayout) findViewById(LAYPADID[i]);

			ivSmall[i].setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					for (int i = 0; i < 16; i++) {
						llPads[i].setBackgroundColor(Color.BLACK);
					}

					switch (v.getId()) {
						case R.id.ivSmall1:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 0;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[0]);
							llPads[0].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 0);
							break;
						case R.id.ivSmall2:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 1;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[1]);
							llPads[1].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 1);
							break;
						case R.id.ivSmall3:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 2;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[2]);
							llPads[2].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 2);
							break;
						case R.id.ivSmall4:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 3;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[3]);
							llPads[3].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 3);
							break;
						case R.id.ivSmall5:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 4;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[4]);
							llPads[4].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 4);
							break;
						case R.id.ivSmall6:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 5;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[5]);
							llPads[5].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 5);
							break;
						case R.id.ivSmall7:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 6;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[6]);
							llPads[6].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 6);
							break;
						case R.id.ivSmall8:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 7;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[7]);
							llPads[7].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 7);
							break;
						case R.id.ivSmall9:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 8;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[8]);
							llPads[8].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 8);
							break;
						case R.id.ivSmall10:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 9;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[9]);
							llPads[9].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 9);
							break;
						case R.id.ivSmall11:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 10;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[10]);
							llPads[10].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 10);
							break;
						case R.id.ivSmall12:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 11;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[11]);
							llPads[11].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 11);
							break;
						case R.id.ivSmall13:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 12;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[12]);
							llPads[12].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 12);
							break;
						case R.id.ivSmall14:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 13;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[13]);
							llPads[13].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 13);
							break;
						case R.id.ivSmall15:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 14;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[14]);
							llPads[14].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 14);
							break;
						case R.id.ivSmall16:
							nOldSOUNDID[nPadIndex] = MainActivity.nSoundID + 15;
							ivPads[nPadIndex].setBackgroundResource(MainActivity.IMAGEIDS[15]);
							llPads[15].setBackgroundColor(Color.GRAY);
							MainActivity.mSoundManager.playSound(MainActivity.nSoundID + 15);
							break;
					}
				}
			});
		}

		ivPads = new ImageView[8];

		for (int i = 0; i < 8; i++) {
			ivPads[i] = (ImageView) findViewById(CHANGEPADID[i]);

			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivPads[i].getLayoutParams();
			params.width = (int) (displayWidth / 7);
			params.height = (int) (displayWidth / 7);
			params.leftMargin = displayWidth / 10 - params.width / 2;
			params.topMargin = displayHeight / 4 - params.width / 2;
			ivPads[i].setLayoutParams(params);
		}

		ivButtons = new ImageView[8];

		for (int i = 0; i < 8; i++) {
			ivButtons[i] = (ImageView) findViewById(BUTTONCHNID[i]);

			ivButtons[i].setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {

					for (int i = 0; i < 8; i++) {
						ivButtons[i].setBackgroundResource(R.drawable.pad_normal);
					}

					v.setBackgroundResource(R.drawable.pad_push);

					for (int i = 0; i < 16; i++) {
						llPads[i].setBackgroundColor(Color.BLACK);
					}

					llPads[nOldSOUNDID[(v.getId() - R.id.btnChange1) / 2] - MainActivity.nSoundID]
							.setBackgroundColor(Color.GRAY);
					nPadIndex = (v.getId() - R.id.btnChange1) / 2;
					MainActivity.mSoundManager.playSound(nOldSOUNDID[(v.getId() - R.id.btnChange1) / 2]);

				}
			});
		}

		llSmall = new LinearLayout[8];

		for (int i = 0; i < 8; i++) {
			llSmall[i] = (LinearLayout) findViewById(LAYSMALLID[i]);

			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llSmall[i].getLayoutParams();
			params.height = (int) (displayWidth / 10);
			llSmall[i].setLayoutParams(params);
		}
	}

	@Override
	protected void onResume() {

		for (int i = 0; i < 8; i++) {
			nOldSOUNDID[i] = MainActivity.SOUNDIDS[i];
		}

		Toast.makeText(this, "Set a sound for each pad.", Toast.LENGTH_LONG).show();

		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder adSave = new AlertDialog.Builder(this);
			adSave.setMessage("Do you want to save the setting?").setCancelable(false)
					.setNegativeButton("No", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							finish();

							dialog.cancel();
						}
					}).setPositiveButton("OK", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							for (int i = 0; i < 8; i++) {
								MainActivity.SOUNDIDS[i] = nOldSOUNDID[i];
							}

							finish();
						}
					});

			AlertDialog ad = adSave.create();
			ad.setTitle("Sound Setting");
			ad.show();
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
